﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ArasPLMExtend
{
    public partial class FrmImportProcess : Form
    {
        public FrmImportProcess()
        {
            InitializeComponent();
        }
        string prdno, cpno;
        public FrmImportProcess(string pno,string compno)
        {
            InitializeComponent();
            prdno = pno;
            cpno = compno;
        }
        private void FrmImportProcess_Load(object sender, EventArgs e)
        {
            startProcess(prdno, "", 1);
            Thread addRelationShip = new Thread(ArasConnection.ArasFunction.AddRelationShip);
            addRelationShip.Start();
            timer1.Start();
        }
        private void startProcess(string pno,string parentPartID,decimal qty)
        {
            Common.PrdInfo mypInfo = null;
            mypInfo = ERPConnection.CERPFunction.getPrdInfoByPrdNo(pno,cpno);
            if(mypInfo!=null)
            {
                //先判断PLM中是否已经存在
                string isHaveID=ArasConnection.ArasFunction.checkPartByPrdNO(pno);
                if (String.IsNullOrEmpty(isHaveID))
                {
                    //不存在添加对应的Part
                    string pid = ArasConnection.ArasFunction.AddPartFromPInfo(mypInfo);
                    if (!String.IsNullOrEmpty(pid))
                    {
                        if (parentPartID != "")
                        {
                            Common.CRelationShip myRelationShip = new Common.CRelationShip();
                            myRelationShip.parentID = parentPartID;
                            myRelationShip.childID = pid;
                            myRelationShip.qty = qty;
                            lock (ArasConnection.PublicState.RelationShipList)
                            {
                                ArasConnection.PublicState.RelationShipList.Add(myRelationShip);
                            }
                        }
                        else
                        {
                            List<Common.CTFBOM> myBOMList = ERPConnection.CERPFunction.getTFBOMList(pno, cpno);
                            if (myBOMList.Count > 0)
                            {
                                foreach (Common.CTFBOM mybom in myBOMList)
                                {
                                    startProcess(mybom.prdno, pid, mybom.pqty);
                                }
                            }
                        }
                    }
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if(ArasConnection.PublicState.RelationShipList.Count==0)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
